package cn.aws360.boot.base.api.pojo.response;

import cn.aws360.boot.base.api.constant.enums.ResTypeEnum;
import cn.hutool.core.date.DateUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author junnan
 */
@Data
@NoArgsConstructor
@ApiModel(value = "返回结果实体")
public class ResultModel<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "结果状态，0-成功")
    private int status;

    @ApiModelProperty(value = "消息提示")
    private String msg;

    @ApiModelProperty(value = "返回结果")
    private T data;

    @ApiModelProperty(value = "响应时间戳")
    private String timestamp;

    public ResultModel(int status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
        this.timestamp = DateUtil.now();
    }

    public static <T> ResultModel<T> res(int status, String msg, T data) {
        return new ResultModel<>(status, msg, data);
    }

    public static <T> ResultModel<T> ok(T data) {
        return new ResultModel<>(ResTypeEnum.OK.getValue(), ResTypeEnum.OK.getMessage(), data);
    }

    public static <T> ResultModel<T> fail(String msg) {
        return new ResultModel<>(ResTypeEnum.FAIL.getValue(), msg, null);
    }

    public static <T> ResultModel<T> error() {
        return new ResultModel<>(ResTypeEnum.ERROR.getValue(), ResTypeEnum.ERROR.getMessage(), null);
    }

}
